<template>
  <div id="app">
    <loading v-model="isLoading"></loading>
    <transition name="fade" mode="out-in">
      <router-view/>
    </transition>
  </div>
</template>

<script>
  import {message} from "./common/util";
  import {weiXinAuth,weiXinRedirect} from "./api";
  import { querystring,Loading } from 'vux'

  export default {
    name: 'App',
    components:{
      Loading
    },
    computed: {
      isLoading(){
        return this.$store.getters.getLoadingStatus
      }
    },
    data(){
      return{
      }
    },
    methods:{
    },
    mounted(){
      let params = querystring.parse(window.location.href.split("?")[1])
      let index = params.index?params.index:querystring.parse(window.location.href.split("?")[2]).index
      sessionStorage.setItem('index', index != null?index:0);//存储数据库标识
      console.log('APP page:',window.location.href,params,index,sessionStorage.getItem('index'))

      return new Promise((resolve,reject)=>{
        if(!(sessionStorage.getItem("access_token") && sessionStorage.getItem("refresh_token") && sessionStorage.getItem("openid") && sessionStorage.getItem("userid") && JSON.parse(sessionStorage.getItem("user"))))
          weiXinAuth(params.code).then((res)=>{
            if(res.status == 'success') {
              console.log("weiXinAuth返回的值：",res.data)
              sessionStorage.setItem('access_token', res.data["access_token"]);
              sessionStorage.setItem('refresh_token', res.data["refresh_token"]);
              sessionStorage.setItem('openid', res.data["openid"]);
              sessionStorage.setItem('userid', res.data["userinfo"]["userid"]);
              sessionStorage.setItem("user", JSON.stringify(res.data["userinfo"]));
              if(!res.data["bindPhone"])sessionStorage.setItem("needBindPhone", true);
              resolve()
            }else if(res.status == 'error'){
              console.log('weiXinAuth error info:',res.msg)
              message({message:res.msg,type: 'error'})
              this.$store.commit('updateLoadingStatus',{isLoading:false})
              this.$vux.alert.show({
                title:'获取微信授权失败',
                content:'请尝试重新获取微信授权，或关闭页面后重新进入系统',
                buttonText:'尝试重新获取',
                onHide () {
                  window.location.href = weiXinRedirect//这里要跳到重定向地址
                }
              })
            }
          }).catch((err)=>{console.log('weiXinAuth catch info:',err)})
        else resolve()
      }).then(()=>{
        this.$store.commit('updateWeiXinInfo',JSON.parse(sessionStorage.getItem("user")))
      })
    },
    created(){
      if(!localStorage.getItem('debugMode'))localStorage.setItem('debugMode',false)
    },
    beforeDestroy(){
    }
  }
</script>

<style lang="scss">
  .fade-enter-active,.fade-leave-active{transition: all 0.2s;}
  .fade-enter,.fade-leave-active{opacity: 0;}
  *{margin:0;padding:0;}
  html{height:100%;color:#2c3e50;font-family: "Helvetica Neue",Helvetica,"PingFang SC","Hiragino Sans GB","Microsoft YaHei","微软雅黑",Arial,sans-serif;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;}
  body{height:100%;background-color: rgb(251, 249, 254);}
  img,a:hover img{border:none;max-width: 100%;}
  a{text-decoration:none;cursor:pointer;color:#2c3e50;}
  ul,li{list-style:none;}
  #app{height:100%;}
  .header{position: fixed !important;z-index:20;top: 0;width: 100%;}
  .topHolder{height:46px;}
  .card-padding{padding:1rem;}
  .weui-cell{
    &.number{
      .weui-cell__ft{font-size: 0.9rem;}
    }
  }
  .listNoItem{width:100%;text-align: center; padding: 1rem 0;color: #a3a3a3;}
  button{
    &.inlineBtn.weui-btn{
      width: auto;display: inline-block;font-size: 0.9rem;margin: 0 0 0 0;
    }
    &.popPickerBtn.weui-btn{
      i{margin-left:0.3rem;font-size: 0.6rem;}
    }
  }
  .btn_popup-picker:before{border-top: none !important;}
  .formPreviewList{
    margin-bottom: 0.5rem;
    .weui-form-preview__hd{
      .weui-form-preview__value{font-size: 1.3rem;}
    }
    &.label{
      .weui-form-preview__hd{
        line-height: 0.5em;
        .weui-form-preview__label{line-height: 1.5em;}
        .weui-form-preview__value {font-size: 0.9rem;display: inline-block;background: #eee;border-radius: 0.3rem;padding: 0 0.6rem;line-height: 1.5rem;}
      }
      .weui-form-preview__bd{
        padding: 0.4rem 1rem;line-height: 1.5rem;
      }
      &.orange{.weui-form-preview__hd{.weui-form-preview__value {background: #fff;border:1px solid #ffca10;color: #ffca10;}}}
      &.green{.weui-form-preview__hd{.weui-form-preview__value {background: #fff;border:1px solid #0abd39;color: #0abd39;}}}
      &.red{.weui-form-preview__hd{.weui-form-preview__value {background: #fff;border:1px solid #ff2623;color: #ff2623;}}}
      &.blue{.weui-form-preview__hd{.weui-form-preview__value {background: #fff;border:1px solid #2775bd;color: #2775bd;}}}
      &.purple{.weui-form-preview__hd{.weui-form-preview__value {background: #fff;border:1px solid #b600ff;color: #b600ff;}}}
      &.lightBlue{.weui-form-preview__hd{.weui-form-preview__value {background: #fff;border:1px solid #78d6f5;color: #78d6f5;}}}
      &.lightGreen{.weui-form-preview__hd{.weui-form-preview__value {background: #fff;border:1px solid #6fbd5b;color: #6fbd5b;}}}
      &.brown{.weui-form-preview__hd{.weui-form-preview__value {background: #fff;border:1px solid #aa8249;color: #aa8249;}}}
      &.gray{.weui-form-preview__hd{.weui-form-preview__value {background: #fff;border:1px solid #bebdbb;color: #bebdbb;}}}
    }
  }
  .vux-table td, .vux-table th{line-height: 1.5rem;padding:0.5rem 0.2rem;}
</style>
